<?php

namespace plugins\member\controller;

use cmf\controller\ApiPluginBaseController; //引用插件基类
use think\Db;
use think\Validate;

class ApiShipAddressController extends ApiPluginBaseController
{

    private function getUid()
    {
        $getIdentity = $this->getIdentity();
        $getIdentity = (array) $getIdentity;
        $uid = $getIdentity['uid'];
        return $uid;
    }

    /**
     * 收获地址列表
     */
    public function addressList()
    {
        $data = $this->request->post();

        $uid = $this->getUid();

        $member = Db::name('member')->where('uid', $uid)->find();

        if (empty($member)) {

            return api_json_echo(false, '用户不存在', null, 101);
        }

        $where = 1;
        if (!empty($data['id'])) {
            $where .= " and id = ".$data['id'];
        }
        if (!empty($data['is_default']) && $data['is_default'] == 1) {
            $where .= " and is_default = 1";
        }

        $list = Db::name('member_ship_address')->field('uid',true)->where('uid', $uid)->where($where)->order('is_default', 'asc')->select()->toArray();

        foreach ($list as $key => $value) {

            $show_address = array();

            $show_address = (array) json_decode($value['show_address']);

            ksort($show_address);

            $list[$key]['show_address'] = array_values($show_address);
        }

        return api_json_echo(true, '查询成功', $list, 200);

    }

    /**
     * 添加收获地址
     */
    public function addAddress()
    {
        $data = $this->request->post();

        $validate = new Validate([
            'receive_name'  => 'require',
            'receive_phone' => 'require',
            'cri_code'      => 'require',
            'address'       => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid = $this->getUid();

        $add = [
            'uid'           => $uid,
            'receive_name'  => $data['receive_name'],
            'receive_phone' => $data['receive_phone'],
            'cri_name'      => !empty($data['cri_name']) ? $data['cri_name'] : '',
            'cri_code'      => !empty($data['cri_code']) ? $data['cri_code'] : '',
            'address'       => $data['address'],
            'postal_code'   => !empty($data['postal_code']) ? $data['postal_code'] : '',
        ];

        $cri_data = Db::name('address')->where('cri_code', $data['cri_code'])->find();

        if (empty($cri_data)) {
            return api_json_echo(false, '无该地区代码对应的信息', null, 101);
        }

        if (!empty($cri_data)) {
            $add['cri_name'] = $cri_data['cri_name'];
        }

        $address = new \plugins\address\model\AddressModel();

        $a = $address->findSuperior($data['cri_code']);

        $b = [];
        foreach ($a as $k => $v) {
            if ($v['cri_level'] == 1 || $v['cri_level'] == 2 || $v['cri_level'] == 3) {
                $b[$v['cri_code']] = $v['cri_name'];
            }
        }

        $add['show_address'] = json_encode($b);

        $member = Db::name('member')->where('uid', $uid)->find();

        if (empty($member)) {
            return api_json_echo(false, '不存在的用户id', null, 101);
        }

        if (!empty($data['is_default'])) {

            $add['is_default'] = $data['is_default'];

            if ($data['is_default'] == 1) {

                //如果设为默认则让所有该用户下的收货地址都变为非默认
                Db::name('member_ship_address')->where('uid', $uid)->update(['is_default' => 2]);
            }
        }
        if (!empty($data['postal_code'])) {

            $add['postal_code'] = (string) $data['postal_code'];
        }

        $add['other'] = '';

        if (!empty($data['other'])) {

            $add['other'] = $data['other'];

        }

        $re = Db::name('member_ship_address')->insert($add);

        if (!$re) {

            return api_json_echo(false, '添加失败', null, 103);

        }

        return api_json_echo(true, '添加成功', $re, 200);

    }

    /**
     * 修改收获地址
     */
    public function editAddress()
    {

        $data = $this->request->post();

        $validate = new Validate([
            'id'            => 'require',
            'receive_name'  => 'require',
            'receive_phone' => 'require',
            'address'       => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid = $this->getUid();

        $edit = [
            'uid'           => $uid,

            'receive_name'  => $data['receive_name'],

            'receive_phone' => $data['receive_phone'],

            'cri_name'      => !empty($data['cri_name']) ? $data['cri_name'] : '',

            'cri_code'      => !empty($data['cri_code']) ? $data['cri_code'] : '',

            'address'       => $data['address'],

            'postal_code'   => !empty($data['postal_code']) ? $data['postal_code'] : '',

        ];

        $cri_data = Db::name('member_ship_address')->where('id', $data['id'])->find();

        if (empty($cri_data)) {

            return api_json_echo(false, '查无该地址id数据', null, 101);

        }

        $edit['cri_name'] = $cri_data['cri_name'];

        $address = new \plugins\address\model\AddressModel();

        $a = $address->findSuperior($data['cri_code']);
        $b = [];
        foreach ($a as $k => $v) {

            if ($v['cri_level'] == 1 || $v['cri_level'] == 2 || $v['cri_level'] == 3) {

                $b[$v['cri_code']] = $v['cri_name'];

            }

        }

        $edit['show_address'] = json_encode($b);

        $member = Db::name('member')->where('uid', $uid)->find();

        if (empty($member)) {

            return api_json_echo(false, '不存在的用户id', null, 101);

        }

        if (!empty($data['is_default'])) {

            $edit['is_default'] = $data['is_default'];

            if ($data['is_default'] == 1) {

                //如果设为默认则让所有该用户下的收货地址都变为非默认

                Db::name('member_ship_address')->where('uid', $uid)->update(['is_default' => 2]);

            }

        }

        if (!empty($data['postal_code'])) {

            $edit['postal_code'] = (string) $data['postal_code'];

        }

        $edit['other'] = '';

        if (!empty($data['other'])) {

            $edit['other'] = $data['other'];

        }

        $re = Db::name('member_ship_address')->where('id', $data['id'])->update($edit);

        if (!$re) {

            return api_json_echo(false, '修改失败', null, 103);

        }

        return api_json_echo(true, '修改成功', $re, 200);

    }

    /**
     * 删除收获地址
     */
    public function delAddress()
    {
        $data = $this->request->post();

        $validate = new Validate([
            'id' => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid = $this->getUid();

        $id = $data['id'];

        $address = Db::name('member_ship_address')->where('id', $id)->find();

        if (empty($data)) {
            return api_json_echo(false, '查无该id数据', null, 101);
        }

        if ($address['uid'] != $uid) {
            return api_json_echo(false, 'id与uid不匹配', null, 101);
        }

        $re = Db::name('member_ship_address')->where('id', $id)->delete();

        if (!$re) {
            return api_json_echo(false, '删除失败', null, 102);
        }

        return api_json_echo(true, '删除成功', $re, 200);

    }

    /**

     * 设置默认地址

     */

    public function setDefaultAddress()
    {
        $post = $this->request->post();

        $uid = $this->getUid();
        $check = ( new \plugins\member\model\MemberModel )->get(['uid'=>$uid]);
        if($check){
            //统一取消默认
            ( new \plugins\member\model\MemberShipAddressModel )->where(['uid'=>$uid])->update(['is_default'=>1]);
            $result = ( new \plugins\member\model\MemberShipAddressModel )->where(['uid'=>$uid,'id'=>$post['id']])->update(['is_default'=>2]);
            if($result){
                return api_json_echo(false, '修改成功');
            }
            return api_json_echo(false, '修改失败');
        }

        return api_json_echo(false, '不存在的用户id');
        
    }

}
